/**
 * @description 表单列表通用mixin
 * */
export default {
  data() {
    return {
      data: [],
      pageIndex: 1,
      pageSize: 10,
      count: 0,
      loading: false,
      prop: '',
      sort: '',
      postVisible: false,
      detailVisible: false,
      opId: 0
    }
  },
  methods: {
    currentChange(pageIndex) {
      this.pageIndex = pageIndex
      this.initData()
    },
    sizeChange(pageSize) {
      this.pageSize = pageSize
      this.onSearch()
    },
    sortChange({ prop, order }) {
      this.prop = prop
      if (order === 'descending') {
        this.sort = 0
      } else {
        this.sort = 1
      }
      this.onSearch()
    },
    onSearch() {
      this.pageIndex = 1
      this.initData()
    },
    reset() {
      this.$refs.searchForm && this.$refs.searchForm.resetFields()
      this.onSearch()
    },
    async initData() {
      const temp = {}
      if (this.searchData !== undefined) {
        Object.assign(temp, this.searchData)
      }
      if (this.prop !== undefined && this.sort !== undefined && this.prop !== '' && this.sort !== '') {
        Object.assign(temp, { sort: this.sort, prop: this.prop })
      }
      this.loading = true
      const { data, count, success, message } = await this.initFunc({ ...temp, pageIndex: this.pageIndex - 1, pageSize: this.pageSize })
      this.loading = false
      if (success) {
        this.data = data
        this.count = count
      } else {
        this.$message({ type: 'success', message })
      }
    },
    toAdd() {
      this.opId = 0
      this.postVisible = true
    },
    toEdit(id) {
      this.opId = id
      this.postVisible = true
    },
    toDetail(id) {
      this.opId = id
      this.detailVisible = true
    },
    toDelete(id) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async() => {
        const { success, message } = await this.delFunc(id)
        if (success) {
          this.$message({ type: "success", message: "删除成功" })
          this.onSearch()
        } else {
          this.$message({ type: "error", message })
        }
      }).catch(() => {

      })
    },
    postClose(flag) {
      if (flag === true) {
        this.initData()
      }
      this.postVisible = false
    },
    detailClose(flag) {
      if (flag === true) {
        this.initData()
      }
      this.detailVisible = false
    }
  },
  mounted() {
    this.initData()
  }
}
